package com.hymjweb.demo.hymj.department_manage.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import com.hymjweb.demo.hymj.department_manage.service.XtDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/pages/department_manage")
public class XtDepartmentController extends BaseController {

    @Autowired
    private XtDepartmentService xtDepartmentService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/department_manage/xtdepartment";
    }

    @RequestMapping("/departmentInit")
    public String departmentInit(HttpServletRequest request) {
        return "pages/department_manage/departmentInit";
    }


    @RequestMapping("/treeQuery")
    @ResponseBody
    public Object treeQuery(@RequestParam(required = false) Map param) throws Exception {
        return xtDepartmentService.treeQuery(param);
    }


    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return xtDepartmentService.getPageList(param);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) XtDepartmentPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("部门添加参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("部门添加成功!");

        int flag = xtDepartmentService.insert(pojo);

        if (flag <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("部门添加失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true) XtDepartmentPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("部门更新参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("部门更新成功!");

        int flag = xtDepartmentService.update(pojo);

        if (flag <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("部门更新失败!");
        }

        return baseResultMap;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("部门删除参数错误");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("部门删除成功!");

        int flag = xtDepartmentService.delete(id);
        if (flag <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("部门删除失败!");
        }

        return baseResultMap;
    }

}
